package com.adxon.servlet;

import com.adxon.MsgException;
import com.adxon.cookie.Station;
import com.adxon.data.Mark;
import com.adxon.data.Item;

import java.io.IOException;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class PostServlet extends ServletBase{
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException{
		Item u=Station.getCurrentUser(req);
		if(u==null)
			forward404(req,resp);
		try{
			String i=null,p=req.getPathInfo();
			if(p!=null && p.length()>1){
				p=p.substring(1);
				int h=p.charAt(0);
				if(h>='0' && h<='9')
					i=p;
			}
			Item o;
			if(i==null){
				o=new Mark();
			}else o=((Item)new Item().update(new StringTokenizer(i,"."))).
					extend();
			System.out.println("PostServlet: "+i+" - "+o.getKind());
			if(req.getParameter("ok")!=null){
				o.setOwner(u);
				try{
					o.setAll(req);
					o.setTime(0);
					o.commit();
					resp.sendRedirect("/");
				}catch(MsgException e){
					e.getMessage(req);
				}
			}else{
				setAttributes(req,"Post");
				o.getPost(req);
				forward(req,resp);
			}
		}catch(MsgException e){
			//e.printStackTrace();
			e.routing(req,resp);
		}
	}
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException{
		System.out.println("doPost");
		doGet(req,resp);
	}
}